package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private static File userDir;
    static {
        userDir = new File("./users");
        if(!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始用户注册！！！！！！！！！！");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);

        if(username==null || username.isEmpty() ||
           password==null || password.isEmpty() ||
           nickname==null || nickname.isEmpty() ||
           ageStr==null   || ageStr.isEmpty()   ||
           !ageStr.matches("[0-9]+")) {
            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);
        File file = new File(userDir,username+".obj");
        if(file.exists()) {
            try {
                response.sendRedirect("/have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (
            ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        ) {
            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+"   "+password);

        if(username==null || password==null) {
            try {
                response.sendRedirect("/login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        File file = new File(userDir,username+".obj");
        if(!file.exists()) {
            try {
                response.sendRedirect("/login_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        User user = null;
        try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
        ) {
            user = (User)ois.readObject();
            if(!user.getPassword().equals(password)) {
                response.sendRedirect("/login_fail.html");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            response.sendRedirect("/login_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response) {
        System.out.println("开始处理用户列表");
        List<User> userList = new ArrayList<>();

        File[] users = userDir.listFiles(f -> f.getName().endsWith(".obj"));
        for(File f : users) {
            try(
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
            ) {
                User user = (User)ois.readObject();
                userList.add(user);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        response.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>\n" +
                    "<html lang=\"en\">\n" +
                    "<head>\n" +
                    "    <meta charset=\"UTF-8\">\n" +
                    "    <title>用户信息</title>\n" +
                    "</head>\n" +
                    "<body>\n" +
                    "  <center>\n" +
                    "    <h1>用户列表</h1>\n" +
                    "    <table>\n" +
                    "      <tr>\n" +
                    "        <td>用户名</td>\n" +
                    "        <td>密码</td>\n" +
                    "        <td>昵称</td>\n" +
                    "        <td>年龄</td>\n" +
                    "      </tr>");
            for(User user : userList) {
                pw.println("<tr>");
                pw.println("<td>"+ user.getUsername() +"</td>");
                pw.println("<td>"+ user.getPassword() +"</td>");
                pw.println("<td>"+ user.getNickname() +"</td>");
                pw.println("<td>"+ user.getAge() +"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>\n" +
                    "  </center>\n" +
                    "</body>\n" +
                    "</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
